<?php
/**
 *
 * MISTwitter  class file
 * 
 * @category   UC
 * @author     张丽媛 <zhangliyuan@snda.com>
 * @copyright  2010-2010 SDO UC Group
 * @version    Release: 1.0.0
 * @link       http://www.sdo.com
 * @modified   2010-5-11 20:20
 */

require_once(dirname(__FILE__).'/../../config/UCConfMacro.php');
require_once(dirname(__FILE__).'/../../config/UCConfTwitter.php');
require_once(dirname(__FILE__).'/../../libraries/UCLibPDOFactory.php');
require_once(dirname(__FILE__).'/../../libraries/UCLibCache.php');
require_once(dirname(__FILE__).'/../../libraries/UCLibMISApi.php');//信息审核
require_once(dirname(__FILE__).'/../user/UCModUserStatistics.php');//用户统计库
require_once(dirname(__FILE__).'/../message/UCModMessageMgr.php');//系统消息
require_once(dirname(__FILE__).'/../feed/UCModFeed.php');//动态接口
require_once(dirname(__FILE__).'/UCModTwitterMap.php');
require_once(dirname(__FILE__).'/UCModHotMoodRecord.php');//热门记录
require_once(dirname(__FILE__).'/UCModUserMoodRecord.php');//用户记录


class MISTwitter{
	static $_errno;

	function __construct(){
	}

	public static function getErrorNo(){
		return self::$_errno;
	}

	/**
	 * @purpose: 修改某条记录的信息
	 * @params string mood_id:记录id
	 * @params string user_id:用户id
	 * @return true or false
	 */
	public static function deleteOneRecordById($mood_id,$user_id){
		if(empty($mood_id) || empty($user_id)){
			self::$_errno = 'Params Error';
			return false;
		}
		//标记删除该条推他信息，并更新cache
		$delete_ret = self::updateOneTwitterByid($mood_id,$user_id);
		if(!$delete_ret){
			self::$_errno = 'UPDATE_DB_ERROR';
			return false;
		}
		
		//给用户发送系统消息
		$o_mgr = new UCModMessageMgr($user_id);
		$o_mgr->sendNotice('rm_twitter',$user_id,array('rm_time'=>mktime()));
	    //var_dump('UCModMessageMgr');

		//删除热门记录
		$hotMoodRecord = new UCModHotMoodRecord();
		$hotMoodRecord-> deleteOneHotMood($mood_id);
		//var_dump('deleteOneHotMood');

		
		//调用map数据库中该条记录的所有转推映射关系todo
		$map_obj  = new UCModTwitterMap($mood_id);
		$map_info = $map_obj ->getOneTwitterMapInfo($user_id);
		//var_dump('getOneTwitterMapInfo');
		//var_dump($map_info);
		$delete_arr = array();
		$delete_arr[] = $mood_id;

		if(is_array($map_info) && count($map_info) >0){
			foreach($map_info as $key =>$value){
				$delete_arr[]= $value['convey_mood_id'];
				//更改各条转推记录的状态，及更新cache todo
				self::updateOneTwitterByid($value['convey_mood_id'],$value['convey_user_id']);
			}
			//var_dump('updateOneTwitterByid over！');
			//置入动态已删除cache todo  改为 调用动态删除接口
			/*var_dump("\n write delete_mood_array =");
			var_dump($delete_arr);
			$cache = new UCLibCache(TWITTER_MCACHE_CHANNEL_NAME);
			var_dump($cache);
			$feed_del_key = UCConfTwitter::getMemcacheDeleteFeedTwitterKey();
			var_dump($feed_del_key);
			$write_ret = $cache->write($feed_del_key,$delete_arr,FEED_DELETE_MCACHE_EXPIRE_TIME);
			$cache->close();
			var_dump("\n write cache result=");
			var_dump($write_ret);
			*/
		}
		return true;;
	}


	/**删除某条记录
	 * @params string mood_id:记录id
	 * @params string user_id:用户id
	 * @return true or false
	 */
	public static function updateOneTwitterByid($mood_id,$user_id){
		$fields = array('status' => 1); 
		$userMoodRecord = new UCModUserMoodRecord($user_id);
		$delete_ret = $userMoodRecord->updateOneRecordById($mood_id,$fields);
		return $delete_ret;
	}
}

/****************TEST CODE***************
$mood_id ='5742efbc5e6111df8b1e000c29d0ebbd';
$user_id ='1109266877';

$ret = MISTwitter::deleteOneRecordById($mood_id,$user_id);
var_dump("\n deleteOneRecordById result=");
if(!$ret){
    $failrason = MISTwitter::getErrorNo();
	var_dump($failrason);
}

$cache = new UCLibCache(TWITTER_MCACHE_CHANNEL_NAME);
$feed_del_key = UCConfTwitter::getMemcacheDeleteFeedTwitterKey();
$delete_mood_array = $cache->read($feed_del_key);
var_dump("\n read delete_mood_array =");
var_dump($delete_mood_array);
$cache->close();

*/
